// $codepro.audit.disable logExceptions
/**
 * This class was created by Nick Popescu to be used in Space Trader
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.JOptionPane;

/**
 * This class is used for saving and loading the game.
 * 
 * @author Nick Popescu
 * @version 1.0 11/12/2012
 * 
 */

public class SaveLoad implements Serializable {
    /**
     * This represents the player to be used in the game
     */
    public Player player;

    /**
     * This represents the space on which the player will move
     */
    public Space space;
    
    /**
     * The out stream.
     */
    public static ObjectOutputStream Out = null;
    
    /**
     * The in stream.
     */
    public static ObjectInputStream In = null;
    
    /**
     * Version number keeps everything synced up.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Saves the game to a file with the given filename.
     * 
     * @param filename
     *            The name of the save file.
     * @throws IOException
     */

    public void save(String filename) {
	try {
	    /*
	     * Create the object output stream for serialization.
	     */

	    this.player = Game.Player;
	    this.space = Game.Space;
	    // this.gameScreen = Game.gameScreen;

	    Out = new ObjectOutputStream( // $codepro.audit.disable assignmentToNonFinalStatic
		    new FileOutputStream(filename));

	    /*
	     * This writes the object.
	     */
	    Out.writeObject(this);
	} catch (FileNotFoundException e) {
	    JOptionPane.showMessageDialog(Game.InitConfigScreen.getFrame(),
		    "Save file not found!");
	} catch (IOException e) {
	    JOptionPane.showMessageDialog(Game.InitConfigScreen.getFrame(),
		    "Something went wrong. General IO error on saving.");
	} finally {
	    
	  try {
	      Out.close();
	  } catch (IOException e) {
	      System.out.println("Couldn't close the file!");
	  }
	    
	}

    }

    /**
     * Loads the game with the given filename.
     * 
     * @param filename
     *            The name of the save file.
     * @return A SaveLoad object which contains the saved information about the
     *         player, space, etc.
     */
    public static SaveLoad load(String filename) {
	SaveLoad saveLoad = null;
	try {

	    In = new ObjectInputStream(
		    new FileInputStream(filename));

	    saveLoad = (SaveLoad) In.readObject();

	    /*
	     * Game.player = saveLoad.player; Game.space = saveLoad.space;
	     * Game.gameScreen = saveLoad.gameScreen;
	     */

	} catch (FileNotFoundException e) {
	    JOptionPane.showMessageDialog(Game.InitConfigScreen.getFrame(),
		    "Load file does not exist!");
	} catch (IOException e) {
	    JOptionPane
		    .showMessageDialog(Game.InitConfigScreen.getFrame(),
			    "Error while loading file! Possibly due to version mismatch.");
	} catch (ClassNotFoundException e) {
	    JOptionPane.showMessageDialog(Game.InitConfigScreen.getFrame(),
		    "Game class not found on loading!");
	}  finally {
	    
	  try {
	      In.close();
	  } catch (IOException e) {
	      System.out.println("Couldn't close the file!");
	  }
	    
	}
	return saveLoad;
    }

    /**
     * This method returns a string representing the SaveLoad class
     * 
     * @return A String representing the SaveLoad class
     */
    public String toString() {
	return this.toString();
    }
}
